<?php
require_once '../include/db.class.php';
require_once '../include/lxbase.class.php';
require_once '../include/alipay.class.php';
require_once '../public/func.php';

$paydata = $_GET["paydata"];
$data = json_decode(base64_decode($paydata),true);

$token = $data['token'];

if (!empty($token)) {
    //session_start();
    //$_SESSION['token'] = $token;
    //setcookie('token', $token, time()+3600);
    $webconn = new Lxbase();
    $temparr = array('token'=>$token, 'sn' => $data["sn"]);
    $ret = $webconn->mongo()->insert('altemp', $temparr, true);
} else {
    die('用户token不能为空！');
}
//echo $mid.'<br/>';

//$rs = $webconn->mongo()->findOne('altemp', array("sn"=>$data['sn']));
//print_r($rs['token']);
//die();
$alipay = new Alipay($data["sn"]);
$alipay->setinit("api");

//企悦用户UID

//订单编号
$sn=$data["sn"];
if(empty($sn)){die("订单编号不能为空");}
//订单标题
$title=$data["title"];
if(empty($title)){die("订单标题不能为空");}
//订单内容
$body=$data["body"];
if(empty($body)){die("订单内容不能为空");}
//订单价格
$price=$data["price"];
if(empty($price)){die("订单价格错误");}
//支付结束跳转链接
$backurl = $data["url"];
if(empty($backurl)){die("回跳地址不能为空");}
//异步通知地址
$nurl = empty($data["nurl"])?$backurl:$data["nurl"];
//客户端IP
$ip=$data["ip"];
if(empty($ip)){die("客户IP不能为空");}

$user = $alipay->viewdata("*","users","where token='{$token}'");
$userid = $user['id'];
$info = $alipay->viewdata("*", "company", "where userid=".$userid);
if(empty($info["name"])){die("用户商户名未设置");}

$paytype = $alipay->viewdata("*", "apuser","where userid={$userid}");
$type = $paytype['alipay_paytype'];

//$type = $data['type'];
if (empty($type)) {$type = 'wap';}
//echo urldecode(base64_decode($backurl));
$host = empty($_SERVER["SERVER_NAME"])?$_SERVER["HTTP_HOST"]:$_SERVER["SERVER_NAME"];
if ($type == 'direct') {
    /**************************请求参数**************************/
    //支付类型
    $payment_type = "1";
    //必填，不能修改
    //服务器异步通知页面路径
    $notify_url = "http://".$host."/api/alipay_notify.php";
    //需http://格式的完整路径，不能加?id=123这类自定义参数
    
    //页面跳转同步通知页面路径
    $return_url = "http://".$host."/api/alipay_return.php";
    //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
    
    //商户订单号
    $out_trade_no = $sn;
    //商户网站订单系统中唯一订单号，必填
    
    //订单名称
    $subject = $title;
    //必填
    
    //付款金额
    $total_fee = $price;
    //必填
    
    //订单描述
    
    $body = $backurl;
    //商品展示地址
    $show_url = $backurl;
    //需以http://开头的完整路径，例如：http://www.商户网址.com/myorder.html
    
    //防钓鱼时间戳
    $anti_phishing_key = "";
    //若要使用请调用类文件submit中的query_timestamp函数
    
    //客户端的IP地址
    $exter_invoke_ip = $ip;
    //非局域网的外网IP地址，如：221.0.0.1
    /************************************************************/
    
    //构造要请求的参数数组，无需改动
    $parameter = array(
        "service" => "create_direct_pay_by_user",
        "partner" => trim($alipay->alipay_config['partner']),
        "seller_email" => trim($alipay->alipay_config['seller_email']),
        "payment_type"	=> $payment_type,
        "notify_url"	=> $notify_url,
        "return_url"	=> $return_url,
        "out_trade_no"	=> $out_trade_no,
        "subject"	=> $subject,
        "total_fee"	=> $total_fee,
        "body"	=> $body,
        "show_url"	=> $show_url,
        "anti_phishing_key"	=> $anti_phishing_key,
        "exter_invoke_ip"	=> $exter_invoke_ip,
        "_input_charset"	=> trim(strtolower($alipay->alipay_config['input_charset']))
    );
    //建立请求
    $html_text = $alipay->buildRequestForm($parameter,"get", "立即支付");
} else if ($type == 'escow') {
    /**************************请求参数**************************/
    
    //支付类型
    $payment_type = "1";
    //必填，不能修改
    //服务器异步通知页面路径
    $notify_url = "http://".$host."/api/alipay_notify.php";
    //需http://格式的完整路径，不能加?id=123这类自定义参数
    
    //页面跳转同步通知页面路径
    $return_url = "http://".$host."/api/alipay_return.php";
    //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
    
    //商户订单号
    $out_trade_no = $sn;
    //商户网站订单系统中唯一订单号，必填
    
    //订单名称
    $subject = $title;
    //必填
    
    //付款金额
    //$price = $price;
    //必填
    
    //商品数量
    $quantity = "1";
    //必填，建议默认为1，不改变值，把一次交易看成是一次下订单而非购买一件商品
    //物流费用
    $logistics_fee = "0.00";
    //必填，即运费
    //物流类型
    $logistics_type = "EXPRESS";
    //必填，三个值可选：EXPRESS（快递）、POST（平邮）、EMS（EMS）
    //物流支付方式
    $logistics_payment = "SELLER_PAY";
    //必填，两个值可选：SELLER_PAY（卖家承担运费）、BUYER_PAY（买家承担运费）
    //订单描述
    
    $body = $backurl;
    //商品展示地址
    $show_url = $backurl;
    //需以http://开头的完整路径，如：http://www.商户网站.com/myorder.html
    
    //收货人姓名
    $receive_name = $info['name'];
    //如：张三
    
    //收货人地址
    $receive_address = $info['province'].$info['city'].$info['district'].$info['address'];
    //如：XX省XXX市XXX区XXX路XXX小区XXX栋XXX单元XXX号
    
    //收货人邮编
    $receive_zip = '000000';
    //如：123456
    
    //收货人电话号码
    $receive_phone = $info['tel'];
    //如：0571-88158090
    
    //收货人手机号码
    $receive_mobile = $info['phone'];
    //如：13312341234
    
    
    /************************************************************/
    
    //构造要请求的参数数组，无需改动
    $parameter = array(
        "service" => "create_partner_trade_by_buyer",
        "partner" => trim($alipay->alipay_config['partner']),
        "seller_email" => trim($alipay->alipay_config['seller_email']),
        "payment_type"	=> $payment_type,
        "notify_url"	=> $notify_url,
        "return_url"	=> $return_url,
        "out_trade_no"	=> $out_trade_no,
        "subject"	=> $subject,
        "price"	=> $price,
        "quantity"	=> $quantity,
        "logistics_fee"	=> $logistics_fee,
        "logistics_type"	=> $logistics_type,
        "logistics_payment"	=> $logistics_payment,
        "body"	=> $body,
        "show_url"	=> $show_url,
        "receive_name"	=> $receive_name,
        "receive_address"	=> $receive_address,
        "receive_zip"	=> $receive_zip,
        "receive_phone"	=> $receive_phone,
        "receive_mobile"	=> $receive_mobile,
        "_input_charset"	=> trim(strtolower($alipay->alipay_config['input_charset']))
    );
    
    //建立请求
    $html_text = $alipay->buildRequestForm($parameter,"get", "立即支付");
} else if ($type == 'wap') {
    //支付类型
    $payment_type = "1";
    //必填，不能修改
    //服务器异步通知页面路径
    $notify_url = "http://".$host."/api/alipay_notify.php";
    //需http://格式的完整路径，不能加?id=123这类自定义参数
    
    //页面跳转同步通知页面路径
    $return_url = "http://".$host."/api/alipay_return.php";
    //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
    
    //商户订单号
    $out_trade_no = $sn;
    //商户网站订单系统中唯一订单号，必填
    
    //订单名称
    $subject = $title;
    //必填
    
    //付款金额
    $total_fee = $price;
    //必填
    
    //商品展示地址
    $show_url = $backurl;
    //必填，需以http://开头的完整路径，例如：http://www.商户网址.com/myorder.html
    
    //订单描述
    $body = $backurl;
    //选填
    
    //超时时间
    $it_b_pay = 0;
    //选填
    
    //钱包token
    $extern_token = '';
    //选填
    
    
    /************************************************************/
    
    //构造要请求的参数数组，无需改动
    $parameter = array(
        "service" => "alipay.wap.create.direct.pay.by.user",
        "partner" => trim($alipay->alipay_config['partner']),
        "seller_id" => trim($alipay->alipay_config['seller_id']),
        "payment_type"	=> $payment_type,
        "notify_url"	=> $notify_url,
        "return_url"	=> $return_url,
        "out_trade_no"	=> $out_trade_no,
        "subject"	=> $subject,
        "total_fee"	=> $total_fee,
        "show_url"	=> $show_url,
        "body"	=> $body,
        "it_b_pay"	=> $it_b_pay,
        "extern_token"	=> $extern_token,
        "_input_charset"	=> trim(strtolower($alipay->alipay_config['input_charset']))
    );
    
    //建立请求
    $html_text = $alipay->buildRequestForm($parameter,"get", "立即支付");
}

//部署到前端展示
if ($type == 'direct') {
    $alipay->smarty->assign("type", '即时到账');
} else if ($type == 'escow') {
    $alipay->smarty->assign("type", '担保交易');
} else if ($type == 'wap') {
    $alipay->smarty->assign("type", '手机WAP');
} 
$alipay->smarty->assign("title", $title);
$alipay->smarty->assign("price", $price);
$alipay->smarty->assign("shopname", $info['name']);
$alipay->smarty->assign("html_text", $html_text);
$alipay->smarty->assign("url",$backurl);
$alipay->smarty->display("alipay.tpl");